package com.moleq.adapter;

import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.moleq.dialog.NetworkStationDialog;
import com.moleq.mgpos.activities.R;
import com.moleq.models.TextAndCheckBox;

public class NetworkReportListAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private int selectedRow = -1;
//	public CheckBox cb_check;
	private List<TextAndCheckBox> listItem;
	private Bitmap activeTrue;
	private Bitmap activeFalse;
	private int mState = NetworkStationDialog.FREE;

	public NetworkReportListAdapter(Context context, List<TextAndCheckBox> listItem) {
		this.mInflater = LayoutInflater.from(context);
		this.listItem = listItem;
		activeTrue = BitmapFactory.decodeResource(context.getResources(), android.R.drawable.presence_online);
		activeFalse = BitmapFactory.decodeResource(context.getResources(), android.R.drawable.presence_invisible);
	}
	
	public void SetSource(List<TextAndCheckBox> listItem){
		this.listItem = listItem;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return listItem.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listItem.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.rpt_station_item,null);
			holder.tv_station_name = (TextView) convertView.findViewById(R.id.tv_station_name);
			holder.tv_state = (TextView) convertView.findViewById(R.id.tv_state);
			holder.cb_check = (CheckBox) convertView.findViewById(R.id.cb_check);
			holder.img_check = (ImageView) convertView.findViewById(R.id.img_net_check);
			holder.ll_check = (LinearLayout) convertView.findViewById(R.id.ll_net_check);
			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}

		String ls_fs_name = String.valueOf(listItem.get(position).getText());
		if (listItem.get(position).isLocal()) {
			ls_fs_name = "*" + ls_fs_name;
		}else {
			ls_fs_name = " " + ls_fs_name;
		}
		holder.tv_station_name.setTextSize(16);
		holder.tv_station_name.setText(ls_fs_name);
		
		holder.tv_state.setText("");
		switch (mState) {
		case NetworkStationDialog.FREE:
			
			break;
		case NetworkStationDialog.BUSY:
			if (listItem.get(position).isState()) {
				holder.tv_state.setText("Done");
			}else {
				holder.tv_state.setText("Retrieving...");
			}
			break;
		case NetworkStationDialog.REPORT_READY:
			if (listItem.get(position).isState()) {
				holder.tv_state.setText("Done");
			}else {
				holder.tv_state.setText("Not Available");
			}
			break;
		}
			
//		if (listItem.get(position).isOnline() || listItem.get(position).isLocal()) {
		if (listItem.get(position).isOnline() || listItem.get(position).isLocal()) {
			holder.img_check.setImageBitmap(activeTrue);
		} else {
			holder.img_check.setImageBitmap(activeFalse);
		}
		
		holder.cb_check.setChecked(false);
		if (listItem.get(position).isCheck()) {
			holder.cb_check.setChecked(true);
		}

		holder.cb_check.setTag(position);
		holder.cb_check.setOnCheckedChangeListener(onCheckedChangeListener);

		return convertView;
	}

	public OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			int index = (Integer) buttonView.getTag();
			listItem.get(index).setCheck(isChecked);
		}
	};

	public final class ViewHolder {
		public TextView tv_station_name;
		public TextView tv_state;
		public CheckBox cb_check;
		public ImageView img_check;
		public LinearLayout ll_check;
	}

	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}

	public int GetSelectedIndex() {
		return selectedRow;
	}

	public void setState(int mState) {
		this.mState = mState;
	}
	
};
